<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/24
 * Time: 17:03
 */
namespace app\api\service;

use app\api\model\SysConfig;
use think\facade\Cache;
use Zzx\config\AliSms;
//use Zzx\Aliyun\Sms\Autoload;
//use Zzx\Aliyun\Sms\DefaultAcsClient;
//use Zzx\Aliyun\Sms\Profile\DefaultProfile;
//use Zzx\Aliyun\Api\Request\V20170525\SendSmsRequest;

class Sms
{
    private $config;

    // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    protected $SignName = "庄钊鑫";

    // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    protected $TemplateCode = "SMS_139239868";

    // fixme 可选: 设置发送短信流水号
    protected $OutId = "12345";

    // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
    protected $SmsUpExtendCode = "1234567";

    public function __construct()
    {
        $config = Cache::get('sms');
        if(!$config){
            $config['access_key'] = SysConfig::where('key','sms_appkey')->value('value');
            $config['access_secret'] = SysConfig::where('key','sms_secretKey')->value('value');
            $config['sign_name'] = $this->SignName;
            Cache::set('sms',$config,3600);
        }
        $this->config = $config;
    }

    /**
     * 发送短信（阿里云短信）
     * @param $mobile  手机号码
     * @param $code    验证码
     * @return bool    短信发送成功返回true失败返回false
     */
    public function sendSmsByAliyun($mobile,$smsParam)
    {
        $alisms = new AliSms();
        $resp = $alisms->sendSms($mobile,$this->TemplateCode,$smsParam,$this->config,$this->OutId);
        //短信发送成功返回True，失败返回false
        if ($resp && $resp->Code == 'OK') {
            return array('status' => 1, 'msg' => '验证码获取成功！'.$resp->Code);
        } else {
            return array('status' => -1, 'msg' => $resp->Message . '. Code: ' . $resp->Code);
        }
    }

}